<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="菜单名称" prop="menuName">
        <el-input
          v-model="queryParams.menuName"
          placeholder="请输入菜单名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="菜单状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:menu:add']">
          新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="initData"></right-toolbar>
    </el-row>

    <el-table v-loading="table.loading" :data="table.data" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="menuName" label="菜单名称" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="icon" label="图标" align="center" width="100">
        <template slot-scope="scope">
          <svg-icon v-if="scope.row.icon" :icon-class="scope.row.icon"/>
        </template>
      </el-table-column>
      <el-table-column prop="orderNum" label="排序" align="center" width="60"></el-table-column>
      <el-table-column prop="perms" label="权限标识" align="center" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="component" label="组件路径" align="center" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <status-tag :status-options="statusOptions" :status="scope.row.status"></status-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-link
            type="primary"
            icon="el-icon-edit"
            :underline="false"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:menu:edit']">
            修改
          </el-link>
          <el-link
            type="primary"
            icon="el-icon-plus"
            :underline="false"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:menu:add']">
            新增
          </el-link>
          <el-link
            type="danger"
            icon="el-icon-delete"
            :underline="false"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:menu:remove']">
            删除
          </el-link>
        </template>
      </el-table-column>
    </el-table>

    <edit ref="edit" :status-options="statusOptions" :menu-options="menuOptions" :system-name-options="systemNameOptions" @success="handleQuery"></edit>
  </div>
</template>

<script>
import {listMenu, delMenu } from "@/api/system/menu";
import Edit from "./components/edit";

export default {
  name: "Menu",
  components: {
    Edit
  },
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 菜单状态数据字典
      statusOptions: [],
      systemNameOptions: [],
      menuOptions: [],
      // 查询参数
      queryParams: {
        menuName: undefined,
        visible: undefined,
        systemNames: undefined
      },
      table: {
        data: [],
        loading: false,
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.initData();
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response;
      });

      this.getDicts("sys_system_name").then(response => {
        this.systemNameOptions = response;
      });
    },
    /** 查询菜单列表 */
    initData() {
      this.table.loading = true;
      listMenu(this.queryParams).then(response => {
        this.table.loading = false;

        const menuTree = this.handleTree(response, "id");
        this.table.data = JSON.parse(JSON.stringify(menuTree));

        this.menuOptions = [];
        const menu = { id: "0", menuName: '主类目', children: [] };
        menu.children = JSON.parse(JSON.stringify(menuTree));
        this.menuOptions.push(menu);
      }).catch(() => {
        this.table.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.initData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.$refs.edit.init(null, row.id, "添加菜单");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.edit.init(row.id, row.parentId, "修改菜单");
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.menuName + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delMenu(row.id);
      }).then(() => {
        this.initData();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
